import {fhirSearchApi, fhirHistoryApi, fhirDeleteApi} from "../../api/modules/fhirApi";
import {createBundleApi, updateBundleApi} from "../../api/modules/bundleApi";

export default {
  state: {
    bundleList: []
  },
  mutations: {
    setBundleList(state, data) {
      state.bundleList = data;
    }
  },
  actions: {
    getBundleList({commit, rootState}, params) {
      return fhirSearchApi(params).then(res => {
        console.log(res);

      if (res.data.total && res.data.entry) {
          commit('setBundleList', res.data.entry)
        } else {
          commit('setBundleList', [{resource:res.data}])
        }

      }).catch(res => {
        commit('setBundleList', []);
      })
    },
    getHistoryBundleList({commit, rootState}, params) {
      return fhirHistoryApi(params).then(res => {
        commit('setBundleList', res.data.entry ? res.data.entry : [{resource: res.data}])
      })
    },
    updateBundle({commit, rootState}, params) {
      return updateBundleApi(params);
    },
    createBundle({commit, rootState}, params) {
      return createBundleApi(params);
    },
    deleteBundle({commit, rootState}, params) {
      return fhirDeleteApi(params);
    }
  }
}
